import { RouteRecordRaw, RouterLink } from "vue-router";
export function mapMenuToRoutes(menus: any[]): RouteRecordRaw[] {
  // 加载所有routes
  const routes: RouteRecordRaw[] = [];
  const allRoutes: RouteRecordRaw[] = [];
  const routeFiles = require.context("../router/main", true, /\.ts$/);
  routeFiles.keys().forEach((key) => {
    console.log(key);
    const route = require("../router/main" + key.split(".")[1]);
    allRoutes.push(route.default);
  });

  //   递归查找 并且拿到需要的route
  const _recurseGetRoutes = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = allRoutes.find((route) => route.path === menu.url);
        route && routes.push(route);
      } else {
        _recurseGetRoutes(menu.children);
      }
    }
  };

  _recurseGetRoutes(menus);
  console.log(routes);
  return routes;
}
